/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;
import java.sql.*;
import java.util.Vector;
import java.sql.Connection;
/**
 *
 * @author PhamLuc
 */
public class Administative {

    private String AcountId;
    private String UserName;
    private String PassWord;
    private String AccountName;
    private String Address;
    private String Email;
    private String Phone;
    private String Authority;

    public String getAcountId() {
        return AcountId;
    }

    public void setAcountId(String AcountId) {
        this.AcountId = AcountId;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String UserName) {
        this.UserName = UserName;
    }

    public String getPassWord() {
        return PassWord;
    }

    public void setPassWord(String PassWord) {
        this.PassWord = PassWord;
    }

    public String getAccountName() {
        return AccountName;
    }

    public void setAccountName(String AccountName) {
        this.AccountName = AccountName;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String Phone) {
        this.Phone = Phone;
    }

    public String getAuthority() {
        return Authority;
    }

    public void setAuthority(String Authority) {
        this.Authority = Authority;
    }
    public static void main(String[] args) {
        System.out.println(Administative.Count());
    }
    public  Administative  Login(String username , String password)
    {
       Administative ad = new Administative();
        int count =0;
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = "select UserName ,PassWord,AccountName,Address,Email,Phone,Authority from Administrative where UserName='"+username+"' and PassWord = HASHBYTES('SHA1' , '"+password+"') ";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {                
                ad.setUserName(rs.getString("UserName"));
                ad.setPassWord(rs.getString("PassWord"));
                ad.setAccountName(rs.getString("AccountName"));
                ad.setAddress(rs.getString("Address"));
                ad.setEmail(rs.getString("Email"));
                ad.setPhone(rs.getString("Phone"));
                ad.setAuthority(rs.getString("Authority"));
                count = 1;
                
            }
            if(count==0)
                {
                    throw new Exception();
                }
        } catch (Exception e) {
            ad=null;
            System.out.println("Error:"+e.getMessage());
        }
        return ad;
    }
    
    public boolean  Change_Password()
    {
      boolean result = false;
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = "update Administrative  set Address='"+this.Address+"' , Email='"+this.Email+"' , PassWord=HASHBYTES('SHA1' , '"+this.PassWord+"') , Phone='"+this.Phone+"' where UserName = '"+this.UserName+"'";
            Statement st = con.createStatement();
            result = st.executeUpdate(query)>0;
        } catch (Exception e) {
            result = false;
            System.out.println(e);
        }
      return  result;
    }
    
    public Administative Infomation_Adminstative(String UserName)
    {
        Administative Ad=new Administative();
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="select * from Administrative where AccountId=? ";
            PreparedStatement pt=con.prepareCall(query);
            pt.setString(1, UserName);
            ResultSet rs=pt.executeQuery();
            while(rs.next())
            {
               
               Ad.setUserName(rs.getString("UserName"));
               Ad.setPassWord(rs.getString("PassWord"));
               Ad.setAccountName(rs.getString("AccountName"));
               Ad.setAddress(rs.getString("Address"));
               Ad.setEmail(rs.getString("Email"));
               Ad.setPhone(rs.getString("Phone"));
               Ad.setAuthority(rs.getString("Authority"));
               Ad.setAcountId(rs.getString("AccountId"));
               
                
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return Ad;
    }
    
    public static Vector AdministativePT()
    {
            Vector vp=new Vector();
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = "select * from Administrative order by AccountId DESC ";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
          
            while (rs.next()) 
            {
                Vector vt = new Vector();
                vt.add(rs.getInt("AccountId"));
                vt.add(rs.getString("UserName"));
                //vt.add(rs.getString("PassWord"));
                vt.add(rs.getString("AccountName"));
                vt.add(rs.getString("Address"));
                vt.add(rs.getString("Email"));
                vt.add(rs.getString("Phone"));
                vt.add(rs.getString("Authority"));
                vp.add(vt);
            }
        } 
        catch (Exception ex) 
        {
            vp = null;
            System.out.println(ex);
        }
        return vp;
    }
    
    
    
    public static Vector AdministativePT1(String a)
    {
            Vector vp=new Vector();
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = "select * from Administrative where UserName like '%"+a+"%' ";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
          
            while (rs.next()) 
            {
                Vector vt = new Vector();
                vt.add(rs.getInt("AccountId"));
                vt.add(rs.getString("UserName"));
                //vt.add(rs.getString("PassWord"));
                vt.add(rs.getString("AccountName"));
                vt.add(rs.getString("Address"));
                vt.add(rs.getString("Email"));
                vt.add(rs.getString("Phone"));
                vt.add(rs.getString("Authority"));
                vp.add(vt);
            }
        } 
        catch (Exception ex) 
        {
            vp = null;
            System.out.println(ex);
        }
        return vp;
    }
    
    
    
    
     public static  Vector SearchAdministative(String UserName)
    {
        Vector Vender = new  Vector(); 
        try
        {//select * from Vender where VanderName like '%"+this.'
            Connection con = ConecttoDatabase.getConnection();
            String query = "select * from Administrative  where UserName like '%"+UserName+"%'";
            Statement st=con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
                Vector vt = new Vector();
                vt.add(rs.getInt("AccountId"));
                vt.add(rs.getString("UserName"));
                //vt.add(rs.getString("PassWord"));
                vt.add(rs.getString("AccountName"));
                vt.add(rs.getString("Address"));
                vt.add(rs.getString("Email"));
                vt.add(rs.getString("Phone"));
                vt.add(rs.getString("Authority"));
                Vender.add(vt);
            }
        }
        catch(Exception ex)
        {
            Vender=null;
            System.out.println(ex);
        }
        
        return Vender;
    }
          public static int Count()
    {
        int co = 0;
        try {
            Connection con = ConecttoDatabase.getConnection();
            String query = " select count(*) from Administrative ";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                co += rs.getInt(1);
            }
        } catch (Exception e) {
           
            System.out.println(e);
        }
        return co;
    }
    public boolean Insert_Administative()
    {
        boolean kq=true;
        try
        {
           Connection con=ConecttoDatabase.getConnection();
           //String query="insert into Administrative values(?,?,?,?,?,?,?)";
           String query="insert into Administrative values('"+this.UserName+"' , HASHBYTES('SHA1', '"+this.PassWord+"') ,'"+this.AccountName+"' ,'"+this.Address+"' , '"+this.Email+"' ,'"+this.Phone+"' ,  '"+this.Authority+"'  )";
           Statement st = con.createStatement();
           kq=st.executeUpdate(query)>0;
          // PreparedStatement pt=con.prepareCall(query);
           //pt.setString(1, this.UserName);
           //pt.setString(2, this.PassWord);
           //pt.setString(3, this.AccountName);
           //pt.setString(4, this.Address);
           //pt.setString(5, this.Email);
           //pt.setString(6, this.Phone);
           //pt.setString(7, this.Authority);
           //kq=pt.executeUpdate()>0;
        }
        catch(Exception ex)
        {
            System.out.println("loi"+ex);
            kq=false;
        }
        return kq;
    }
    public boolean Edit_Administative(int id)
    {
        boolean kq=true;
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="Update Administrative set PassWord='"+this.PassWord+"',AccountName='"+this.AccountName+"',Address='"+this.Address+"',Email='"+this.Email+"',Phone='"+this.Phone+"',Authority='"+this.Authority+"' where AccountId='"+this.AcountId+"' ";
            Statement st=con.createStatement();
            kq=st.executeUpdate(query)>0;
        }
        catch(Exception ex)
        {
            System.out.println("bi loi o day"+ex);
            kq=false;
        }
        return kq;
    }
    public static Administative Identical(String str)
    {
        Administative ad=new Administative();
        boolean b=false;
        int count=0;
        try
        {
            Connection con=ConecttoDatabase.getConnection();
            String query="select UserName from Administrative where UserName='"+str+"'";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
            while(rs.next())
            {
                ad.setUserName(rs.getString("UserName"));
                count=1;
            }
             if(count==0)
             {
                 throw new Exception();
             }
            
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            ad=null;
        }
        return ad;
    }
     public static boolean Delete_Administative(int id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query="delete from Administrative where AccountId=?";
             PreparedStatement pt=con.prepareCall(query);
             pt.setInt(1, id);
             kq=pt.executeUpdate()>0;
         }
         catch(Exception ex)
         {
             System.out.println(ex);
         }
         return kq;
     }
     public static boolean KTInsert_admin(String user)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query="select UserName from Administrative where UserName='"+user+"'";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(user.equals(rs.getString("UserName")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
                 {
                     System.out.println(ex);
                 }
         return kq;
     }
     public static boolean KTDelete_Administative(String id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query=" select AccountId from SalesBill where AccountId='"+id+"' ";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(id.equals(rs.getString("AccountId")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
         {
             System.out.println(ex);
            
         }
         return kq;
     }
     
       public static boolean KTDelete_Administative1(String id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query=" select AccountId from PurchaseBill where AccountId='"+id+"' ";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(id.equals(rs.getString("AccountId")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
         {
             System.out.println(ex);
            
         }
         return kq;
     }
         
          public static boolean KTDelete_Administative2(String id)
     {
         boolean kq=true;
         try
         {
             Connection con=ConecttoDatabase.getConnection();
             String query=" select AccountId from CallManagement where AccountId='"+id+"' ";
             Statement st=con.createStatement();
             ResultSet rs=st.executeQuery(query);
             while(rs.next())
             {
                 if(id.equals(rs.getString("AccountId")))
                 {
                     kq=false;
                     break;
                 }
             }
             
         }
         catch(Exception ex)
         {
             System.out.println(ex);
            
         }
         return kq;
     }
                
}
